package com.zx.dsptuneup_TriMic.activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ExpandableListView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.orhanobut.hawk.Hawk;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.zx.dsptuneup_TriMic.Adapter.ReplaceChildAdapter;
import com.zx.dsptuneup_TriMic.Adapter.ReplaceParentAdapter;
import com.zx.dsptuneup_TriMic.Adapter.StrategyAdapter;
import com.zx.dsptuneup_TriMic.Base.BaseActivity;
import com.zx.dsptuneup_TriMic.R;
import com.zx.dsptuneup_TriMic.Tools.CVIPaperDialogUtils;
import com.zx.dsptuneup_TriMic.Tools.Constant;
import com.zx.dsptuneup_TriMic.Util.StringUtil;
import com.zx.dsptuneup_TriMic.Util.ToastUtil;
import com.zx.dsptuneup_TriMic.bean.BasicResponse;
import com.zx.dsptuneup_TriMic.bean.ChildDeviceBean;
import com.zx.dsptuneup_TriMic.bean.DeviceBean;
import com.zx.dsptuneup_TriMic.network.DefaultObserver;
import com.zx.dsptuneup_TriMic.network.NetWorkManager;
import com.zx.dsptuneup_TriMic.widget.ui.EmptyView;
import com.zx.dsptuneup_TriMic.widget.ui.LoadingView;
import com.zx.dsptuneup_TriMic.widget.ui.RecyclerViewEmptySupport;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import butterknife.BindView;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

/**
 * @author cyj
 * @createDate 2021-11-08
 * @ClassName: ReplaceActivity
 * @Description: 替换
 * @Version: 1.0
 */
public class ReplaceActivity extends BaseActivity {
    @BindView(R.id.ic_nav)
    TextView nav_left;
    @BindView(R.id.listView)
    RecyclerViewEmptySupport listView;
    @BindView(R.id.inc_empty_view)
    EmptyView mIEmptyView;
    @BindView(R.id.inc_loading_view)
    LoadingView mILoadingView;
    @BindView(R.id.refreshLayout)
    RefreshLayout mRefreshLayout;     // 下拉刷新

    /**
     * currentType 当前类型 true:主设备 false:子设备
     * */
    private boolean currentTypeParent = false;
    private ReplaceParentAdapter replaceParentAdapter;
    private ReplaceChildAdapter replaceChildAdapter;
    private List<DeviceBean> ReplaceParentlist = new ArrayList<>();         // 替换的主设备列表
    private List<ChildDeviceBean> ReplaceChildlist = new ArrayList<>();     // 替换的子设备列表
    private String deviceId = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_replace);

        Intent intent = getIntent();
        deviceId = intent.getStringExtra(Constant.addDevicesID);
//        deviceId="fdabde0c3ebb4f09b43b32b972d1bfc5";
        currentTypeParent = intent.getBooleanExtra(Constant.addDevicesType,false);
        initUI();
        initData();

    }

    private void initUI() {
        mILoadingView.showLoading();
        mIEmptyView.setEmptyTip("暂无数据");
        mIEmptyView.setVisibility(View.GONE);
        listView.setVisibility(View.GONE);
        mRefreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                initData();
            }
        });

        nav_left.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });

        if(currentTypeParent){
            replaceParentAdapter = new ReplaceParentAdapter(this,ReplaceParentlist);
            // 替换按钮点击
            replaceParentAdapter.setOnReplaceClickListener(new ReplaceParentAdapter.OnReplaceParentClickListener() {
                @Override
                public void OnReplaceParentClick(int position) {
                    if(deviceId == null){
                        ToastUtil.makeText(ReplaceActivity.this,"未选择要替换的设备!");
                        return;
                    }
                    String mac = ReplaceParentlist.get(position).getMac();
                    CVIPaperDialogUtils.showCustomDialog(ReplaceActivity.this, "确定要替换以下设备吗?", "设备mac:\n"+mac, "确定", false, new CVIPaperDialogUtils.ConfirmDialogListener() {
                        @Override
                        public void onClickButton(boolean clickConfirm, boolean clickCancel) {
                            if (clickConfirm) {
                                replaceMainDevice(deviceId,mac);
                            }
                        }
                    });
                }
            });
            listView.setLayoutManager(new LinearLayoutManager(this));
            listView.setAdapter(replaceParentAdapter);
        }else {
            replaceChildAdapter = new ReplaceChildAdapter(this,ReplaceChildlist);
            // 替换按钮点击
            replaceChildAdapter.setOnReplaceClickListener(new ReplaceChildAdapter.OnReplaceChildClickListener() {
                @Override
                public void OnReplaceChildClick(int position) {
                    if(deviceId == null){
                        ToastUtil.makeText(ReplaceActivity.this,"未选择要替换的设备!");
                        return;
                    }
                    String mac = ReplaceParentlist.get(position).getMac();
                    CVIPaperDialogUtils.showCustomDialog(ReplaceActivity.this, "确定要替换以下设备吗?", "设备mac:\n"+mac, "确定", false, new CVIPaperDialogUtils.ConfirmDialogListener() {
                        @Override
                        public void onClickButton(boolean clickConfirm, boolean clickCancel) {
                            if (clickConfirm) {
                                replaceChildDevice(deviceId,mac);
                            }
                        }
                    });
                }
            });
            listView.setLayoutManager(new LinearLayoutManager(this));
            listView.setAdapter(replaceChildAdapter);
        }
    }


    private void initData() {
        if(currentTypeParent){
            NetWorkManager.getInstance().getNetWorkApiService().getParentDevices().compose(ReplaceActivity.this.<BasicResponse<List<DeviceBean>>>bindToLifecycle())
                    .subscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(new DefaultObserver<BasicResponse<List<DeviceBean>>>() {
                        @Override
                        protected void onSuccess(BasicResponse<List< DeviceBean >> response) {
                            if (response != null) {
                                mRefreshLayout.finishRefresh();
                                mILoadingView.hideLoading();

                                ReplaceParentlist.clear();
                                ReplaceParentlist = response.getData();
                                if(ReplaceParentlist.size() > 0){
                                    mIEmptyView.setVisibility(View.GONE);
                                    listView.setVisibility(View.VISIBLE);
                                }else {
                                    mIEmptyView.setVisibility(View.VISIBLE);
                                    listView.setVisibility(View.GONE);
                                }
                                replaceParentAdapter.setGridViewBeanList(ReplaceParentlist);
                            }
                        }

                        @Override
                        protected void onFail(BasicResponse<List<DeviceBean>> response) {
                            super.onFail(response);
                            mRefreshLayout.finishRefresh();
                            mILoadingView.hideLoading();
                        }

                        @Override
                        public void onError(@NonNull Throwable e) {
                            super.onError(e);
                            mRefreshLayout.finishRefresh();
                            mILoadingView.hideLoading();
                        }
                    });
        }else {
            NetWorkManager.getInstance().getNetWorkApiService().getChildDevices().compose(ReplaceActivity.this.<BasicResponse<List<ChildDeviceBean>>>bindToLifecycle())
                    .subscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(new DefaultObserver<BasicResponse<List<ChildDeviceBean>>>() {
                        @Override
                        protected void onSuccess(BasicResponse<List<ChildDeviceBean>> response) {
                            if (response != null) {
                                mRefreshLayout.finishRefresh();
                                mILoadingView.hideLoading();

                                ReplaceChildlist.clear();
                                ReplaceChildlist = response.getData();
                                if(ReplaceChildlist.size() > 0){
                                    mIEmptyView.setVisibility(View.GONE);
                                    listView.setVisibility(View.VISIBLE);
                                }else {
                                    mIEmptyView.setVisibility(View.VISIBLE);
                                    listView.setVisibility(View.GONE);
                                }
                                replaceChildAdapter.setGridViewBeanList(ReplaceChildlist);
                            }
                        }

                        @Override
                        protected void onFail(BasicResponse<List<ChildDeviceBean>> response) {
                            super.onFail(response);
                            mRefreshLayout.finishRefresh();
                            mILoadingView.hideLoading();
                        }

                        @Override
                        public void onError(@NonNull Throwable e) {
                            super.onError(e);
                            mRefreshLayout.finishRefresh();
                            mILoadingView.hideLoading();
                        }
                    });
        }
    }

    /**
     * 替换主设备
     * */
    private void replaceMainDevice(String oldId, String mac){
        NetWorkManager.getInstance().getNetWorkApiService().replaceMainDevice(oldId,mac).compose(ReplaceActivity.this.<BasicResponse<String>>bindToLifecycle())
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new DefaultObserver<BasicResponse<String>>() {
                    @Override
                    protected void onSuccess(BasicResponse<String> response) {
                        if (response != null) {
                            ToastUtil.makeText(ReplaceActivity.this,"替换成功!");
                            new Handler().postDelayed(new Runnable() {
                                @Override
                                public void run() {
                                    finish();
                                }
                            },300);
                        }
                    }
                    @Override
                    protected void onFail(BasicResponse<String> response) {
                        super.onFail(response);
                    }
                    @Override
                    public void onError(@NonNull Throwable e) {
                        super.onError(e);
                    }
                });
    }

    /**
     * 替换子设备
     * */
    private void replaceChildDevice(String oldId, String mac){
        NetWorkManager.getInstance().getNetWorkApiService().replaceChildDevice(oldId,mac).compose(ReplaceActivity.this.<BasicResponse<String>>bindToLifecycle())
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new DefaultObserver<BasicResponse<String>>() {
                    @Override
                    protected void onSuccess(BasicResponse<String> response) {
                        if (response != null) {
                            ToastUtil.makeText(ReplaceActivity.this,"替换成功!");
                            new Handler().postDelayed(new Runnable() {
                                @Override
                                public void run() {
                                    finish();
                                }
                            },300);
                        }
                    }
                    @Override
                    protected void onFail(BasicResponse<String> response) {
                        super.onFail(response);

                    }
                    @Override
                    public void onError(@NonNull Throwable e) {
                        super.onError(e);
                    }
                });
    }

}
